package org.xxpay.pay.ctrl.payment;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.xxpay.core.common.constant.PayConstant;
import org.xxpay.core.common.util.MyBase64;
import org.xxpay.core.common.util.MyLog;
import org.xxpay.core.entity.PayOrder;
import org.xxpay.pay.channel.gepay.GepayController;
import org.xxpay.pay.ctrl.common.BaseController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.regex.Pattern;

/**
 * @Description: 支付下单界面跳转
 * @date 2020年8月20日09:57:52
 * @version V1.0
 */
@Controller
public class TopayController extends BaseController {

	private static final MyLog _log = MyLog.getLog(GepayController.class);

	/**
	 * 跳转至通道方收银台
	 * @param request
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/api/pay_{payOrderId}.htm")
	public String toPay(HttpServletRequest request, ModelMap model, @PathVariable String payOrderId) {
		// 获取支付订单号
        JSONObject json = new JSONObject();
        json.put("code", PayConstant.RETURN_VALUE_FAIL);
        // 查询该订单
        PayOrder payOrder = commonService.payOrderService.findByPayOrderId(payOrderId);
        if (payOrder == null) {
            json.put("message", "下单失败，订单不存在");
            model.put("errMsg", json);
            _log.info("下单失败，订单不存在！payOrderId={}", payOrderId);
            return "common/pc_error";
        }

        // 判断订单状态
        if (payOrder.getStatus() == PayConstant.PAY_STATUS_SUCCESS || payOrder.getStatus() == PayConstant.PAY_STATUS_COMPLETE) {
            return "notify/show";
        }

        if (payOrder.getStatus() != PayConstant.PAY_STATUS_INIT && payOrder.getStatus() != PayConstant.PAY_STATUS_PAYING) {
            json.put("message", "下单失败，订单状态错误");
            model.put("errMsg", json);
            _log.info("下单失败，订单状态错误！payOrderId={}, status={}", payOrderId, payOrder.getStatus());
            return "common/pc_error";
        }

        if (StringUtils.isEmpty(payOrder.getTopayUrl())) {
            json.put("message", "下单失败，下单地址为空");
            model.put("errMsg", json);
            _log.info("下单失败，下单地址为空！payOrderId={}", payOrderId);
            return "common/error";
        }

        String jumpForm = payOrder.getTopayUrl();
        if(StringUtils.isBlank(jumpForm)) {
            jumpForm = "生成的跳转地址为空,请联系管理员";
        }else {
            jumpForm = isBase64(jumpForm) ? new String(MyBase64.decode(jumpForm)) : jumpForm;
        }

        model.put("jumpForm", jumpForm);
		return "payment/jump";
	}

    private static boolean isBase64(String str) {
        String base64Rule = "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$";
        return Pattern.matches(base64Rule, str);
    }

}
